<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactEmployeeTag\Action\Dashboard;

use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\Rbac\Middleware\PermissionMiddleware;
use MoChat\App\User\Contract\UserContract;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\ContactEmployeeTag\Contract\ContactEmployeeTagContract;

/**
 * 客户画像 - 添加客户个人标签.
 *
 * Class Store
 * @Controller
 */
class Index extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var UserContract
     */
    protected $userContract;

    /**
     * @Inject
     * @var StdoutLoggerInterface
     */
    protected $logger;

    /**
     * @Inject
     * @var ContactEmployeeTagContract
     */
    private $contactEmployeeTagService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     *     @Middleware(PermissionMiddleware::class)
     * })
     * @RequestMapping(path="/dashboard/contactEmployeeTag/index", methods="GET")
     */
    public function handle()
    {
        $user = user();

        // 判断用户绑定企业信息
        if (! isset($user['corpIds'])) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未选择登录企业，不可操作');
        }
        // 接收参数
        $params = [
            'contactId' => (int) $this->request->input('contactId'),
            'employeeId' => (int) $this->request->input('employeeId'),
        ];
        $this->validated($params);
        // 所有标签
        $list = $this->contactEmployeeTagService->getContactEmployeeTagsByEmployeeId($params['employeeId'], ['id', 'name']);
        // 客户个人标签
        $contactTag = $this->contactEmployeeTagService->getContactEmployeeTagsByContactIdEmployeeId($params['contactId'], $params['employeeId']);
        $contactTagIds = empty($contactTag) ? [] : array_column($contactTag, 'id');
        foreach ($list as $k => $v) {
            $list[$k]['isSelect'] = 0;
            if (! empty($contactTagIds) && in_array($v['id'], $contactTagIds, true)) {
                $list[$k]['isSelect'] = 1;
            }
        }
        return $list;
    }

    /**
     * @return string[] 规则
     */
    public function rules(): array
    {
        return [
            'contactId' => 'required|integer|min:1|bail',
            'employeeId' => 'required|integer|min:1|bail',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    public function messages(): array
    {
        return [
            'contactId.required' => '客户id 必传',
            'employeeId.required' => '员工id 必传',
        ];
    }
}
